﻿using Microsoft.EntityFrameworkCore;
using NewFormatProject.Infrastructure.Atrributes;
using NewFormatProject.Infrastructure.Dto.Datas;
using NewFormatProject.Infrastructure.Dto.Requests.Objects;
using NewFormatProject.Infrastructure.Dto.Responses;
using NewFormatProject.Infrastructure.Dto.Responses.Objects;
using NewFormatProject.Infrastructure.EFCore;
using NewFormatProject.Server.EFCore;
using NewFormatProject.Server.Instances.Extends.Entitys;
using NewFormatProject.Server.Instances.Extends.Requests;
using NewFormatProject.Server.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewFormatProject.Server.Instances
{
    internal class JobIOC {
        public JobServerEF jobServerEF { get; set; }
    }
    [InjectClass]
    public class JobServer : IJobServer
    {
        private readonly JobIOC _jobIOC;
        private readonly DbContext _dbContext;
        public JobServer(DbContext dbContext)
        {
            _dbContext = dbContext;
            _jobIOC = new JobIOC
            {
                jobServerEF = new JobServerEF(dbContext)
            };
        }
        public async Task<ResponseApiDto> Add(RequestJobsDto dto)
        {
           return ResponseApiDto.Create(await _jobIOC.jobServerEF.Add(dto.ToEntity()));
        }

        public async Task<ResponseApiDto> Delete(RequestJobsDto dto)
        {
            return ResponseApiDto.Create(await _jobIOC.jobServerEF.Delete(dto.ToEntity()));
        }

        public async Task<ResponseApiDto> Detail(int id)
        {
            return ResponseApiDto.Success("数据已获取",(await _jobIOC.jobServerEF.Query(d=>d.JId==id).SingleOrDefaultAsync()).ToResponseDto());
        }

        public async Task<ResponseApiDto> GetJobByDepartmentId(int depid)
        {
            return ResponseApiDto.Success("数据已获取",(await _jobIOC.jobServerEF.Query(d=>d.JDepartmentId== depid).ToListAsync()).Select(d=>d.ToResponseDto()));
        }

        public async Task<ResponseApiDto> Pagination(int page, int limit, string key)
        {
            List<ResponseJobsDto> result = new List<ResponseJobsDto>();
            int total = 0;
            List<Jobs> jobs = await _jobIOC.jobServerEF.Pagination1(d=>string.IsNullOrEmpty(key)||d.JName.Contains(key),false,d=>d.JCreateTime,out total,page,limit).ToListAsync();
            jobs.ForEach(job =>
            {
                result.Add(job.ToResponseDto());
            });
            return ResponseApiDto.Success("数据已获取",new PaginationData
            {
                data= result,
                total= total
            });
        }

        public async Task<ResponseApiDto> Update(RequestJobsDto dto)
        {
            return ResponseApiDto.Create(await _jobIOC.jobServerEF.UpdateAllProperNotNull(dto.ToEntity()));
        }
    }
}
